<template>
  <div class="target-class" v-loading="loading">
    <template v-if="gradeEnumList.length">
      <p class="search">
        <el-select
          class="select-grade"
          v-model="currentGrade"
          @change="resetList"
        >
          <el-option
            v-for="item in gradeEnumList"
            :key="item.value"
            :label="item.label"
            :value="item.id"
          ></el-option>
        </el-select>
      </p>
      <el-table class="table" :data="list">
        <el-table-column
          prop="class_id"
          label="编号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="members"
          label="人数"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <p class="operator">
              <span @click="deleteClass(scope.row)"
                ><img
                  class="disband"
                  src="@/assets/images/class-manage/disband-dark-icon.svg?url"
                />解散</span
              >
              <span @click="showDetail(scope.row)"
                ><img
                  class="eye"
                  src="@/assets/images/class-manage/eye-icon.svg?url"
                />查看详情</span
              >
            </p>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        layout="prev, pager, next"
        :total="total"
        :page-size="page.size"
        :current-page="page.current"
        @current-change="changePage"
      />
      <disband-class
        v-model="visible"
        :id="currentClassId"
        @confirm-disband="confirmDelete"
      ></disband-class>
    </template>
    <div v-else class="empty">
      <img src="@/assets/images/assignment/wrong-question-tip.png" />
      <p class="tip">暂无靶向班班级数据，请在任意选择学生处创建</p>
    </div>
  </div>
</template>
<script setup>
import DisbandClass from './DisbandClass.vue'
import { getTargetList, deleteTargetClass } from '@/apis/class-manage/index'
import { ref, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores'

const authStore = useAuthStore()
const router = useRouter()
const loading = ref(false)

const showDetail = row => {
  console.log('show')
  router.push({
    path: '/classManage/target/detail',
    query: {
      classId: row.class_id,
      className: row.name,
      gradeId: row.grade_id
    }
  })
}

// 解散班级
const visible = ref(false)
const currentClassId = ref('')
const deleteClass = row => {
  currentClassId.value = row.class_id
  visible.value = true
}

const resetList = () => {
  page.current = 1
  getList()
}

// 靶向班列表
const page = reactive({
  current: 1,
  size: 15
})
const currentGrade = ref()
const list = ref([])
const total = ref(0)
const getList = async () => {
  const { data } = await getTargetList({
    school_id: authStore.userInfo.school_id,
    teacher_id: authStore.userInfo.userId,
    grade_id: currentGrade.value,
    page: String(page.current),
    page_size: String(page.size)
  })
  list.value = data?.list || []
  total.value = data?.total || 0
}

const changePage = val => {
  page.current = val
  getList()
}

const gradeEnumList = ref([])
onMounted(async () => {
  loading.value = true
  const { gradeList, classList } = await authStore.getUserInfoFun()
  const cachedClassId = sessionStorage.getItem('ai-learn-class-manage-classId')
  const cachedClass =
    cachedClassId && classList.find(v => v.value === cachedClassId)
  const isMatchCache =
    cachedClass && gradeList.map(v => v.value).includes(cachedClass.grade_num)
  gradeEnumList.value = gradeList
  currentGrade.value = isMatchCache
    ? cachedClass.grade_id
    : gradeEnumList.value[0]?.id
  // 爱学班角色查看靶向班处理
  if (authStore.nowIsSmartRoomCode) {
    const grades = Array.from(new Set(classList.map(v => v.grade_num)))
    gradeEnumList.value = grades.map(num => {
      const target = classList.find(item => item.grade_num === num)
      return {
        value: target.grade_num,
        label: target.grade_name,
        id: target.grade_id
      }
    })
    currentGrade.value = gradeEnumList.value[0]?.id
  }
  if (currentGrade.value) {
    await getList()
  }
  loading.value = false
})

const confirmDelete = () => {
  page.current = 1
  getList()
}
</script>
<style lang="less" scoped>
.target-class {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .empty {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 600px;

    img {
      width: 300px;
    }

    .tip {
      color: #333;
      font-size: 14px;
      margin-top: 20px;
    }
  }

  .search {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
  }

  .select-grade {
    width: 200px;

    :deep(.el-select__wrapper) {
      height: 36px;
      border-radius: 4px;
      box-shadow: unset;
      border: 1px solid #b2cdff;

      * {
        color: #b2cdff;
      }
    }
  }

  .table {
    .operator {
      color: @color-brand-regular;

      span {
        display: inline-flex;
        gap: 8px;
        align-items: center;
        cursor: pointer;

        .disband {
          width: 12px;
        }

        .eye {
          width: 15px;
        }

        &:first-child {
          margin-right: 20px;
        }
      }
    }
  }

  .pagination {
    justify-content: center;
    margin-top: auto;
  }

  :deep(.el-table) {
    thead {
      background-color: #f2f4f9;
      tr {
        background-color: #f2f4f9;
        color: #040b1af2;

        font-size: 16px;
        border-radius: 14px 0;

        th {
          padding: 17px 0;

          &:first-child {
            border-radius: 15px 0 0 0;
          }

          &:last-child {
            border-radius: 0 15px 0 0;
          }

          &:not(:last-child):after {
            content: '';
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            height: 16px;
            width: 1px;
            background: #1c2230;
          }
        }

        .el-table__cell {
          border-bottom: unset;

          .cell {
            font-weight: 500;
          }
        }
      }
    }

    &__cell {
      background-color: unset;
      padding: 10px 0;
    }
  }

  :deep(.disband-dialog) {
    .el-dialog__header {
      font-size: 24px;
      font-weight: 700;
      color: #040b1a;
    }

    .el-dialog__body {
      .tip {
        font-weight: 500;
        font-size: 18px;
      }

      .btn-box {
        text-align: center;
        margin-top: 20px;

        .btn {
          margin-left: 20px;
          background-color: #fff;
          border: 1px solid @color-brand-regular;
          border-radius: 3px;
          font-size: 16px;
          font-weight: 700;
          padding: 7px 20px;
        }

        .confirm {
          background-color: @color-brand-regular;
          color: #fff;
        }

        .cancel {
          color: @color-brand-regular;

          &:hover {
            background-color: #ecf5ff;
          }
        }
      }
    }
  }
}
</style>
